/**************************************************************
 *@ProjectName: blweibo
 *@FileName: GFActivity.java
 *@Date: 2011-5-31
 *@Copyright: 2011 www.hrbanlv.com Inc. All rights reserved.
 **************************************************************/
package com.zhiweibo;

import java.util.ArrayList;
import java.util.List;

import android.app.ProgressDialog;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.ListView;

import com.zhiweibo.models.User;
import com.zhiweibo.util.Constant;
import com.zhiweibo.util.HttpUtil;

/**
 * The Class GFActivity.
 * 
 * @author SunHaibin
 * @since Android 1.6
 * @history 2011-5-31
 */
public class GFActivity extends BaseActivity implements OnItemClickListener {

    /** The lv gf. */
    private ListView lvGF;

    /** The m list. */
    private List<User> mList = new ArrayList();

    /** The adapter. */
    private GfAdapter adapter;

    /*
     * (non-Javadoc)
     * 
     * @see com.yingnet.zhiweibo.BaseActivity#handleTitleBarEvent(int)
     */
    @Override
    protected void handleTitleBarEvent(int paramInt) {
        if (paramInt == 1) {
            finish();
        } else if (paramInt == 0) {
            lvGF.setSelection(0);
            new FetchGF().execute(0);
        }
    }

    /*
     * (non-Javadoc)
     * 
     * @see com.yingnet.zhiweibo.BaseActivity#onCreate(android.os.Bundle)
     */
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setView(R.layout.listbase);
        setTitleBar(1, getString(R.string.imageviewer_back), getString(R.string.weibo_officialweibo),
                getString(R.string.main_reload));
        lvGF = (ListView) findViewById(R.id.lvUser);
        adapter = new GfAdapter();
        lvGF.setAdapter(adapter);
        lvGF.setOnItemClickListener(this);
        new FetchGF().execute(0);

    }

    /**
     * The Class FetchGF.
     * 
     * @author SunHaibin
     * @since Android 1.6
     * @history 2011-5-31
     */
    class FetchGF extends AsyncTask<Object, Integer, Object> {

        /** The pd. */
        private ProgressDialog pd;

        /*
         * (non-Javadoc)
         * 
         * @see android.os.AsyncTask#doInBackground(Params[])
         */
        @Override
        protected Object doInBackground(Object... params) {
            mList = HttpUtil.getUsers(GFActivity.this, Constant.HOT + "?type=58");
            return null;
        }

        /*
         * (non-Javadoc)
         * 
         * @see android.os.AsyncTask#onPostExecute(java.lang.Object)
         */
        @Override
        protected void onPostExecute(Object result) {
            adapter.notifyDataSetChanged();
            if (pd != null) {
                pd.dismiss();
            }
        }

        /*
         * (non-Javadoc)
         * 
         * @see android.os.AsyncTask#onPreExecute()
         */
        @Override
        protected void onPreExecute() {
            pd = new ProgressDialog(GFActivity.this);
            pd.setMessage("请稍候……");
            pd.show();
        }

    }

    /**
     * The Class GfAdapter.
     * 
     * @author SunHaibin
     * @since Android 1.6
     * @history 2011-5-31
     */
    class GfAdapter extends BaseAdapter {

        /*
         * (non-Javadoc)
         * 
         * @see android.widget.Adapter#getCount()
         */
        @Override
        public int getCount() {
            return mList.size();
        }

        /*
         * (non-Javadoc)
         * 
         * @see android.widget.Adapter#getItem(int)
         */
        @Override
        public Object getItem(int position) {
            return mList.get(position);
        }

        /*
         * (non-Javadoc)
         * 
         * @see android.widget.Adapter#getItemId(int)
         */
        @Override
        public long getItemId(int position) {
            return position;
        }

        /*
         * (non-Javadoc)
         * 
         * @see android.widget.Adapter#getView(int, android.view.View,
         * android.view.ViewGroup)
         */
        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            if (convertView == null) {
                return new UserListItemView(GFActivity.this, mList.get(position));
            } else {
                ((UserListItemView) convertView).update(GFActivity.this, mList.get(position));
                return convertView;
            }
        }

    }

    /*
     * (non-Javadoc)
     * 
     * @see
     * android.widget.AdapterView.OnItemClickListener#onItemClick(android.widget
     * .AdapterView, android.view.View, int, long)
     */
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        startActivity(new Intent(GFActivity.this, UserInfoActivity2.class).putExtra("user", mList.get(position)));
    }
}
